package com.tianma315.admin.produce.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.ResponseUtil;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.produce.VO.ScAdditiveVO;
import com.tianma315.admin.produce.dto.ScAdditiveDto;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.produce.domain.ScAdditiveDO;
import com.tianma315.admin.produce.service.ScAdditiveService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 配添加剂(生产)
 * </pre>
 * <small> 2019-05-30 09:53:17 | Aron</small>
 */
@Controller
@RequestMapping("/produce/scAdditive")
public class ScAdditiveController extends BaseController{
	@Autowired
	private ScAdditiveService scAdditiveService;
	
	@GetMapping()
//	@RequiresPermissions("produce:scAdditive:scAdditive")
	String ScAdditive(){
	    return "produce/scAdditive/scAdditive";
	}

	@GetMapping("/{produceTaskId}")
	String ScAdditive(@PathVariable("produceTaskId") Integer produceTaskId,Model model){
		List<ScAdditiveDO> scAdditiveDOList = scAdditiveService.getScAdditiveDO(produceTaskId);
		Boolean flag = false;
		if (scAdditiveDOList==null || scAdditiveDOList.isEmpty()){
			flag = true;
		}
		model.addAttribute("produceTaskId",produceTaskId);
		model.addAttribute("flag",flag);
		return "produce/scAdditive/scAdditive";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("produce:scAdditive:scAdditive")
	public Result<Page<ScAdditiveVO>> list(Integer pageNumber, Integer pageSize, ScAdditiveDO scAdditiveDTO){
		// 查询列表数据
        Page<ScAdditiveVO> page = scAdditiveService.getScAdditiveVO(pageNumber,pageSize,scAdditiveDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
//	@RequiresPermissions("produce:scAdditive:add")
	String add(){
	    return "produce/scAdditive/add";
	}

	@GetMapping("/add/{produceTaskId}")
	String add(@PathVariable("produceTaskId")Integer produceTaskId,Model model){
		model.addAttribute("produceTaskId",produceTaskId);
		return "produce/scAdditive/add";
	}

	@GetMapping("/edit/{additiveId}")
//	@RequiresPermissions("produce:scAdditive:edit")
	String edit(@PathVariable("additiveId") Integer additiveId,Model model){
		ScAdditiveVO scAdditive = scAdditiveService.selectVOById(additiveId);
		model.addAttribute("scAdditive", scAdditive);
	    return "produce/scAdditive/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("produce:scAdditive:add")
	public Result<String> save(@RequestBody ScAdditiveDto scAdditive){
		scAdditive.setCreateBy(getUserId());
		scAdditive.setCreateDate(Calendar.getInstance().getTime());
		if(scAdditiveService.addScAdditiveDO(scAdditive)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("produce:scAdditive:edit")
	public Result<String>  update( @RequestBody ScAdditiveDto scAdditive){
		scAdditiveService.updateDtoById(scAdditive);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("produce:scAdditive:remove")
	public Result<String>  remove( Integer additiveId){
		if(scAdditiveService.deleteScAdditiveDO(additiveId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
//	@RequiresPermissions("produce:scAdditive:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] additiveIds){
		for (int i = 0; i < additiveIds.length; i++) {
			scAdditiveService.deleteScAdditiveDO(additiveIds[i]);
		}
		return Result.ok();
	}
	@RequestMapping("/material/select")
	ModelAndView productSelect() {
		ModelAndView view = new ModelAndView("produce/scAdditive/material-select");
		return view;
	}

	/**
	 * 导出模板
	 */
	@GetMapping( "/exportAdditive/{produceTaskId}")
	public void exportPurchase(@PathVariable Integer produceTaskId,HttpServletResponse response){
		String fileName = "添加剂导入模板.xls";
		try {
			response = ResponseUtil.getResponse(response,fileName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HSSFWorkbook wb =scAdditiveService.exportAdditive(produceTaskId);
		try(OutputStream os = response.getOutputStream()) {
			wb.write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 进入导入excel
	 */
	@GetMapping( "/additiveImport")
	public String purchaseImport(MultipartFile file){
		return "produce/scAdditive/additiveImport";
	}

	/**
	 * 导入excel
	 */
	@ResponseBody
	@PostMapping( "/importAdditive")
	public Result importPurchase(MultipartFile file){
		if (!file.isEmpty()){
			scAdditiveService.importAdditive(file,getUserId());

		}
		return Result.ok();
	}
}
